<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    body {
      background-color: skyblue;
    }
    .box1 {
      position: absolute;
      width: 100px;
      height: 100px;
      background-color: red;
    }
  </style>
  <body>
    <div class="box1"></div>
    <script>
      var box1 = document.querySelector(".box1");
      document.onkeydown = function (event) {
        var speed = 5;
        if (event.ctrlKey) {
          speed = 50;
        }
        switch (event.keyCode) {
          case 87:
            box1.style.top = box1.offsetTop - speed + "px";
            // console.log("top");
            break;
          case 83:
            box1.style.top = box1.offsetTop + speed + "px";
            break;
          case 65:
            box1.style.left = box1.offsetLeft - speed + "px";
            break;
          case 68:
            box1.style.left = box1.offsetLeft + speed + "px";
            break;
        }
      };

      //按键被按下
      //   document.onkeydown = function () {
      //     console.log("111");
      //   };
      //按键被松开
      //   document.onkeyup = function () {
      //     console.log("222");
      //   };
    </script>
  </body>
</html>
